<?php
/*
 * 支付回调
 */
namespace Mobile\Controller;

use Mobile\Model\orderModel;

use Mobile\Controller\PaywechatController;

class ScancallbackController extends BaseController {

    public $orderModel;
    public $payModel;

    public function __construct() {
        parent::__construct();
        $this->orderModel = new orderModel();
        $this->payModel = M('pay');
    }
    

   

    //扫码回调
    public function index(){
    
        $wechatpay = new PaywechatController();

        
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $xml_arr = $wechatpay->xmlToArray($xml);
        $res = $wechatpay->checkSign($xml_arr);
        
        if($res == FALSE){
            $qrcodeApiObj['return_code'] = 'FAIL';
            $qrcodeApiObj['return_msg'] = '支付失败';
            $qrcodeApiObj["result_code"] = 'FAIL';
            echo $wechatpay->arrayToXml($qrcodeApiObj);exit;
        }
        
        //创建本地订单
        $openid = $xml_arr['openid'];
        //查看是否是系统会员
        $uid = $this->_check_openid($openid);
        //查找订单
        $order_sn = $xml_arr['product_id'];
        
        //从订单号里面剔除后缀  _qrpay
        //$order_sn = str_replace($order_sn, "_qrpay", '');
        
        $order = $this->orderModel->getOrder(array("order_sn"=>$order_sn));
        if(empty($order)){
            exit;
        }
        
        $pay_xml = $this->_get_wechatpay($order,$openid);
    }
    
    
    /*
     * 生成微信支付信息
     * 1订单,2充值
     */
    protected function _get_wechatpay($order,$openid){
    
        $notify_url = get_domain()."/mobile/ordercallback/callback/order_id/".$order['order_id'];// 异步通知地址
    
        $wechatpay = new PaywechatController();
        $wechatpay->setParameter("notify_url", $notify_url);//通知URL
        $wechatpay->setParameter("body", "订单:".$order['order_sn']."_qrpay");//商品描述
        $wechatpay->setParameter("out_trade_no", $order['order_sn']."_qrpay");//商户订单号
        $wechatpay->setParameter("total_fee", $order['pay_money']*100);//订单总金额,单位为分
        $wechatpay->setParameter("spbill_create_ip", $order['order_ip']);//客户ip地址
        $wechatpay->setParameter("openid",$openid);//
        $wechatpay->setParameter("trade_type","NATIVE");//
        
        $wechatpay->returnQrcodexml($order);
    }
    
    

    /*
     * 根据openid获取用户信息,注册/查找用户
     */
    protected function _check_openid($openid){
        
        $uid = 0;
        $local = $this->getMemberInfoByOpenid($openid);
        
        if(empty($local)){
            //未注册
            //未注册,新注册用户
            $user_data['status'] = 1;
            $user_data['last_login_ip'] = get_client_ip();
            $user_data['last_login_time'] = date("Y-m-d H:i:s");
            $user_data['login_times'] = 1;
            $user_data['create_time'] = date("Y-m-d H:i:s");
            $user_data['role_id'] = 2;
            $user_data['is_change_username'] = 0;
            $user_data['subscribe'] = 0;//未关注
            $user_data['openid'] = $openid;
             
            $user_data['regfrom'] = "qrpay";//账号来源

            if($user_data['openid']){
                $uid = $this->memberModel->addMember($user_data);
            }
        
        }else{
            //已注册
            $uid = $local['uid'];
        }
        
        return $uid;
        
    }
    
    public function alipayqrcode(){
    
    
    
        $order_id = get('order_id');

        
        $order = $this->orderModel->where(array('order_id'=>$order_id))->find();
        $alipay = new PayaliwapController();
        $data['notify_url'] = get_domain().'/mobile/ordercallback/alipaycallback/order_id/'.$order_id;//异步通知
        
        $data['return_url'] = get_domain().'/mobile/orderdetail/index/order_id/'.$order_id;//同步通知
        
        //用户付款中途退出返回商户网站的地址
        $data['show_url'] = get_domain().'/mobile/orderdetail/index/order_id/'.$order_id;
        
        $data['out_trade_no'] = $order['order_sn'];//商城订单号
        $data['subject'] = '订单'.$order['order_sn'];//商品名称
        // $data['body'] = '';//商品描述
        $data['total_fee'] = $order['pay_money'];//交易金额
        $url = $alipay->wappay($data);
    
        qrcode($url);
    
    }



    public function qrcode(){
        
        $order_sn = get("order_sn");
        $type = get('type');
        
        
        if(!$order_sn){
            exit("order_sn error");
        }
        
        $pay_url = $this->_getPayUrl($order_sn, $type);
        
        qrcode($pay_url);
    
    }
    
    
    protected function _getPayUrl($order_sn,$type){
        
        $orderWhere = array(
            "order_sn" => $order_sn,
        );
        $payOrderWhere = array(
            "order_sn" => $order_sn,
            'pay_type' => $type
        );
        
        $order = $this->orderModel->getOrder($orderWhere);
        
        if($order['checked'] != 0){
            return false;
        }
        
        //若订单存在切未过期 直接返回url
        $payorder = $this->payModel->where($payOrderWhere)->find();
        
        if($payorder){
            
            if($payorder['expired_time']>date("Y-m-d H:i:s") && $payorder['code_url']!=null){
                return $payorder['code_url'];
            }
        }
      
        $payHm = new PayhmController();
        if($type == 5){
            $notifyurl = get_domain().'/mobile/ordercallback/hmcallback/order_sn/'.$payorder['order_sn'];
            $res = $payHm->wechatCreateOrder($order,$notifyurl);
        }elseif($type == 3){
            
            $res = $payHm->alipayCreateOrder($order);
        }
        
        
        /*  $data = array(
         "out_trade_no" => $out_trade_no,//返回的外部订单号
         "code_url" => $code_url,//二维码内容
         ); */
        $data = array(
            'uid' => $order['uid'],
            'code_url' => $res['code_url'],
            'out_trade_no' => $res['out_trade_no'],
            'order_sn' => $order['order_sn'],
            'order_id' => $order['order_id'],
            'pay_type' => 5,
            'create_time' => date("Y-m-d H:i:s"),
            'expired_time' => date("Y-m-d H:i:s",strtotime('+3600 second')),
        );
        
        if($payorder){
            $this->payModel->where($payOrderWhere)->save($data);
        }else{
            $this->payModel->add($data);
        }
        //$this->orderModel->where($where)->setField('transaction_id',$res['out_trade_no']);
        return $res['code_url'];
    }
    
    
    

    
    
}